package com.lotter.pay;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.lotter.common.util.RoomUtil;

public class Alipay {
	private final static String APP_ID = "2016081201740589";
	private static AlipayClient alipayClient = null;
	public final static String  APP_PRIVATE_KEY="MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAMeosO/EWyTzPzP7z7p4ANy0+MKHj9v1SvZtvaxLDMAVSdk3ycsDIacI6BWoBUdn1Tpv6YQrsQc1BYbcw4YxGrSETYB/UmbE4/I2Kl4NLxsjYiECULCAUK3LDdvbkcVfdRjKlZYr4nfHOWEV345fO9yMJSqny0GBQfycTlBS9jwfAgMBAAECgYAvS0I+SWDdq9D421klbm+l2Chky//DtqL/JJedTFnE1kYb8h8Wu7MpAe8X0ikN9QaJRSEnccIJVwHsJDccfl04oizV9c2mOZ7Wwn8ouOHqncbjNd+6ij5q7C4OP29aCCcR8+M8IHzTFDfPAdSfxS7wDELCjeFONxtgOwj9dZHwgQJBAO5PDOnY1C4jL9DncQF+s8qE6FBN/X9y/LTA4nIZx5c+5BqeH5P+0CGeiITdcHb2enTTgrIjahoDGgr0JmvRpQ8CQQDWex5AQgglWJk2h0afNBKoD2cN6eP7HIjfiShrKEja/itFnZ9L821RpzGWr+a7osP+EzWR/SETlEBTl15d4pfxAkB/uhLby4F98RIRz9p7yKMoqq+xSvvRsZnMb41vRm35nJW5xoJ6WhrHyia1v9IlkNn1f+APBng51OvCbzWgmE71AkBJoA/dXQ1wzH4jssIyamyxFkB5DT3+eGgf2/Rs/HIuAqwWBKqnJtPzAKLMKGvbI1+GO0/bYnpVP0Hpr/9r2RnxAkBV73rwOPIkkPei2StT/R3oxcdtzeZ0yQDaNfR2fTiMb6FZmwpUqgzRcogp9d1LGewuEwkq3TJsHSj82BIj4Gzz";
	public final static String ALIPAY_PUBLIC_KEY="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDI6d306Q8fIfCOaTXyiUeJHkrIvYISRcc73s3vF1ZT7XN8RNPwJxo8pWaJMmvyTn9N4HQ632qJBVHf8sxHi/fEsraprwCtzvzQETrNRwVxLO5jVmRGi60j8Ue1efIlzPXV9je9mkjzOmdssymZkh2QhUrCmZYI/FCEa3/cNMW0QIDAQAB";
	
	static {
		alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json",
				"UTF-8", ALIPAY_PUBLIC_KEY);
	}
	public static void pay(HttpServletRequest httpRequest,
		 	           HttpServletResponse httpResponse,double total,
		 	           String subject,String returnUrl) throws Exception {
		AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();// 创建API对应的request
		if(StringUtils.isNotBlank(returnUrl)){
			alipayRequest.setReturnUrl(returnUrl);
		}
		 alipayRequest.setNotifyUrl("http://www.xssst.com/ja/website/pay/alicalbk");//在公共参数中设置回跳和通知地址
		JSONObject json = new JSONObject();
		json.put("out_trade_no",  RoomUtil.findNextVal("flow"));
		json.put("total_amount", total);
		json.put("subject", subject);
		json.put("seller_id", "2088221540260600");
		json.put("product_code", "Cai Piao");
		alipayRequest.setBizContent(json.toJSONString());// 填充业务参数
		String form = alipayClient.pageExecute(alipayRequest).getBody();
		httpResponse.setContentType("text/html;charset=UTF-8");
		httpResponse.getWriter().write(form);// 直接将完整的表单html输出到页面
		httpResponse.getWriter().flush();

	}

	/**
	 * 支付宝回调处理
	 */
	public static AlipayCalReq callbackReq(Map<String, String> map) throws Exception {
		boolean signVerified = AlipaySignature.rsaCheckV1(map, ALIPAY_PUBLIC_KEY, "UTF-8"); // 调用SDK验证签名

		//httpResponse.setContentType("text/html;charset=UTF-8");

		AlipayCalReq calReq = new AlipayCalReq();
		if (signVerified) {
			calReq.setCheckSign(true);
		}else {
			calReq.setCheckSign(false);
		}
		calReq.setOut_trade_no((String) map.get("out_trade_no"));// 原始交易流水号-平台产生的

		calReq.setTrade_no((String) map.get("trade_no"));// 支付宝交易凭证号

		calReq.setTrade_status((String) map.get("trade_status"));

		calReq.setTotal_amount((String)map.get("total_amount"));// 订单金额（元）
		return calReq;
	/*	httpResponse.getWriter().write(result);// 直接将完整的表单html输出到页面
		httpResponse.getWriter().flush();*/

	}
}
